package cn.py.app;
/***
 * @modify
 * ===>2013.7.31
 *      1.取消验证码；
 *      2.修改登录界面
 * **/

import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.cloud.httputil.HttpUtil;
import com.dream.myqiyi.MainActivity;
import com.dream.myqiyi.R;
import com.dream.myqiyi.home.HomeActivity;

public class NiActivity extends Activity {
    /** Called when the activity is first created. */
    private static final String TAG="NiActivity";
    private static final int LOGINFO=0x10000;
    private static final int NETWORKWRONG=0x20000;
    private static final int LOGINSUC=0x0001;
    private EditText editText;
//    private EditText editText1;
//    private ImageView imageView;
//    private Button button;
    private String c;
//    public String cookie;
    private static String cardid;
    public static  String SESSIONID =null;
    public static  String UNITNO =null;
    private JSONObject jsonObject;
    private ProgressBar progressBar;
	//cardid=5101123456&code=7650
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login);
        editText=(EditText)findViewById(R.id.user);
       /*==>2013.7.31
       editText1=(EditText)findViewById(R.id.code);
        imageView=(ImageView)findViewById(R.id.imageview);
    	cookie=getView();*/
        Button button=(Button)findViewById(R.id.button);
        progressBar=(ProgressBar)findViewById(R.id.progressBar_login);
        button.setOnClickListener(listener);
      //  button.setOnClickListener(new OnClickListener());
        Log.i(TAG,"================login start==================");
         
 
    }

  /*==>2013.7.31
    private String getView() {
    	URL url;
		try {
			url = new URL("http://125.70.9.164:1026/app/code.jsp");
			 CookieManager manager = new CookieManager();  
		     CookieHandler.setDefault(manager);
		     HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			 connection.setRequestMethod("GET");
			 connection.setConnectTimeout(5000);
			 int code = connection.getResponseCode();
			 Object   obj = connection.getContent();
		     CookieStore cookieJar = manager.getCookieStore();
		     List<HttpCookie> cookies = cookieJar.getCookies();
	         for (HttpCookie cookie : cookies) {
		      c=cookie.toString();
		     }
			 if (code == 200) {
				InputStream in = connection.getInputStream();
				Bitmap bm = BitmapFactory.decodeStream(in);
				imageView.setImageBitmap(bm);
			 }
			System.out.println(c);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return c;
		
	}*/

    View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cardid=editText.getText().toString();
          //  if(!"".equals(cardid)&&cardid.length()==18){
              if(!"".equals(cardid)){
            	progressBar.setVisibility(View.VISIBLE);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            jsonObject=HttpUtil.login(cardid);
                            if(jsonObject!=null){
                                SESSIONID = jsonObject.getString("sessionid");
                                UNITNO = jsonObject.getString("unitno");
                                Log.i(TAG,SESSIONID + "===" + UNITNO);
                                int s=jsonObject.getInt("status");
                                Log.i(TAG,"s:"+s);
                                Message message=new Message();
                                message.what=LOGINFO;
                                message.arg1=s;
                                handler.sendMessage(message);
                            }else{
                                handler.sendEmptyMessage(NETWORKWRONG);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            handler.sendEmptyMessage(NETWORKWRONG);
                        }
                    }
                }).start();

            }else{
                Toast.makeText(NiActivity.this,R.string.pleaninputcorrectData,Toast.LENGTH_SHORT).show();
            }

        }
    };

  Handler handler=new Handler(){
      @Override
      public void handleMessage(Message msg) {
          progressBar.setVisibility(View.GONE);
          switch (msg.what){
              case LOGINFO:
                  switch (msg.arg1){
                      case 1:
                          Intent intent=new Intent();
                          intent.setClass(getApplicationContext(), MainActivity.class);
                          setResult(LOGINSUC,intent);
                          finish();
                          break;
                      case 0:
                          Toast.makeText(NiActivity.this, R.string.login_fail, 1).show();
                          break;
                  }
                  break;
              case NETWORKWRONG:
                  Toast.makeText(NiActivity.this, R.string.networkwrong, Toast.LENGTH_SHORT).show();
                  break;
          }
          super.handleMessage(msg);
      }
  };


} 